<template>
  <div v-loading="loading" class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('项目分摊规则') }}</span>
    </div>
    <el-form ref="dealForm" :model="dealForm" :rules="rules" label-width="120px" class="dataForm" style="height: 90px;">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('分摊方式')" prop="shareWay">
            <dict-select
              v-model="dealForm.shareWay"
              dictionary="fund_share_way"
              :placeholder="$t('分摊方式')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('尾差处理')" prop="tailPoor">
            <dict-select
              v-model="dealForm.tailPoor"
              dictionary="fund_tail_poor"
              :placeholder="$t('尾差处理')"
            />
          </el-form-item>
        </el-col>
        <!--<el-col :span="24">-->
        <!--  <el-form-item :label="$t('备注')" prop="remark">-->
        <!--    <el-input-->
        <!--      v-model="dealForm.remark"-->
        <!--      type="textarea"-->
        <!--      maxlength="2000"-->
        <!--      show-word-limit-->
        <!--      :placeholder="$t('备注')"-->
        <!--    />-->
        <!--  </el-form-item>-->
        <!--</el-col>-->
      </el-row>
    </el-form>

    <div class="area-header">
      <span class="area-header-title">{{ $t('费用分摊规则') }}</span>
    </div>
    <el-form ref="costForm" :model="costForm" :rules="rules" label-width="120px" class="dataForm">
      <el-table :data="costForm.ruleList" border class="nowrapTable formTable">
        <el-table-column :label="$t('费用类型')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('费用类型') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'ruleList.' + scope.$index + '.feeType'" :rules="rules.feeType">
              <dict-select
                v-model="scope.row.feeType"
                dictionary="fund_expense_type_fee"
                :placeholder="$t('费用类型')"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('费用分摊规则')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('费用分摊规则') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'ruleList.' + scope.$index + '.feeRule'" :rules="rules.feeRule">
              <dict-select
                v-model="scope.row.feeRule"
                dictionary="fund_share_way"
                :placeholder="$t('费用分摊规则')"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('备注') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'ruleList.' + scope.$index + '.remark'" :rules="rules.remark">
              <el-input v-model="scope.row.remark" type="textarea" class="fileTextarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
            </el-form-item>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: center;margin-top: 11px;">
        <!--保存 取消按钮-->
        <el-button v-has-permi="['share:rules:edit']" type="primary" @click="submitForm">{{ $t('保存') }}</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getFundShareRulesByFundId, saveFundShareRules } from '@/api/fund/share/rules/fundShareRules'
import { getData, saveFundFeeSharingRule } from '@/api/fund/feesharingrule/fundFeeSharingRule'

export default {
  data() {
    return {
      // 表单参数
      dealForm: {},
      // 表单参数
      costForm: {
        ruleList: [],
        fundId: this.$route.query.id
      },
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        feeType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        feeRule: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        shareWay: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        tailPoor: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
      },
      queryForm: {
        fundId: this.$route.query.id
      }
    }
  },
  created() {
    this.getDicts('fund_tail_poor').then(response => {
      this.tailPoorOptions = response.data
    })
    this.getDicts('fund_share_way').then(response => {
      this.shareWayOptions = response.data
    })
    this.init()
  },
  methods: {
    // 初始化数据
    init() {
      this.loadDeal()
      this.loadCost()
    },
    loadDeal() {
      getFundShareRulesByFundId(this.$route.query.id).then(response => {
        this.dealForm = response.data
      })
    },
    loadCost() {
      this.loading = true
      getData(this.queryForm).then(response => {
        this.costForm = response.data
        this.loading = false
      })
    },
    // 表单重置
    resetDeal() {
      this.dealForm = {
        id: undefined,
        fundId: undefined,
        tailPoor: undefined,
        shareWay: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined
      }
      this.resetForm('dealForm')
    },
    // 表单重置
    resetCost() {
      this.costForm = {
        ruleList: []
      }
      this.resetForm('costForm')
    },
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
        this.$parent.$parent.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      const _this = this
      var saveFlagCost = false
      var saveFlag = false
      _this.$refs['costForm'].validate(valid => {
        if (valid) {
          saveFlagCost = true
        }
      })
      _this.$refs['dealForm'].validate(valid => {
        if (valid) {
          saveFlag = true
        }
      })
      if (saveFlag && saveFlagCost) {
        this.$confirm(this.$t('确定保存?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          _this.saveRule()
          _this.saveCost()
        })
      }
    },
    saveCost() {
      this.$refs['costForm'].validate(valid => {
        if (valid) {
          this.loading = true
          this.costForm.fundId = this.$route.query.id
          saveFundFeeSharingRule(this.costForm).then(response => {
            if (response.code === 200) {
              console.log(this.$t('保存成功'))
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    saveRule() {
      this.$refs['dealForm'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFundShareRules(this.dealForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
            } else {
              this.msgError(response.msg)
            }
            this.closeForm()
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }

  }

}
</script>
